#
# This is a base koji_builder role. 
#
- name: set root passwd
  user: name=root password={{ builder_rootpw }} state=present
  tags:
  - koji_builder

- name: add mock user as 425
  user: name=mock uid=425 state=present home=/var/lib/mock createhome=yes system=yes
  tags:
  - koji_builder

- name: make mock homedir perms
  file: state=directory path=/var/lib/mock mode=2775 owner=root group=mock
  tags:
  - koji_builder

- name: add mock ssh dir
  file: state=directory path=/var/lib/mock/.ssh mode=700 owner=mock group=mock
  tags:
  - koji_builder

- name: add mock ssh keys
  copy: src=mock_auth_keys dest=/var/lib/mock/.ssh/authorized_keys mode=640 owner=mock group=mock
  tags:
  - koji_builder

- name: add kojibuilder
  user: name=kojibuilder groups=mock 
  tags:
  - koji_builder

- name: add mockbuilder
  user: name=mockbuilder groups=mock
  tags:
  - koji_builder

- name: mockbuilder .ssh dir
  file: state=directory path=/home/mockbuilder/.ssh mode=700 owner=mockbuilder group=mockbuilder
  tags:
  - koji_builder

- name: mockbuilder ssh key
  copy: src=ftbfs_auth_keys dest=/home/mockbuilder/.ssh/authorized_keys mode=644 owner=mockbuilder group=mockbuilder
  tags:
  - koji_builder

- name: make a bunch of dirs
  file: state=directory path={{ item }}
  with_items:
    - /pub
    - /mnt/fedora_koji
    - /pub/fedora
    - /pub/epel
  tags:
  - koji_builder

- name: make a bunch of dirs
  file: state=directory path={{ item }} owner=apache group=apache
  with_items:
    - /mnt/koji/packages
    - /mnt/koji/repos
    - /mnt/koji/work
    - /mnt/koji/scratch
  when: env == 'staging'
  tags:
  - koji_builder

- name: add pkgs
  yum: state=present pkg={{ item }}
  with_items:
    - yum-utils
    - koji-builder
    - strace
    - mock
    - kernel-firmware
    - ntp
    - ntpdate
    - rsyslog
    - audit
  tags:
  - koji_builder
  when: ansible_distribution_major_version|int < 22

- name: add oz/imagefctory pkgs on all but PPC EPEL
  yum: state=present pkg={{ item }}
  with_items:
    - oz
    - imagefactory
    - imagefactory-plugins-TinMan
    - imagefactory-plugins-Docker
    - imagefactory-plugins-vSphere
    - imagefactory-plugins-ovfcommon
    - imagefactory-plugins
    - imagefactory-plugins-OVA
    - imagefactory-plugins-EC2
    - imagefactory-plugins-RHEVM
    - python-psphere
    - VMDKstream
    - pykickstart
  when: not inventory_hostname.startswith('buildppc-') and ansible_distribution_major_version|int < 22
  tags:
  - koji_builder

- name: add pkgs
  dnf: state=present pkg={{ item }}
  with_items:
    - yum-utils
    - koji-builder
    - strace
    - mock
    - kernel-firmware
    - ntp
    - ntpdate
    - rsyslog
    - audit
  tags:
  - koji_builder
  when: ansible_distribution_major_version|int > 21

- name: add oz/imagefctory pkgs on all but PPC EPEL
  dnf: state=present pkg={{ item }}
  with_items:
    - oz
    - imagefactory
    - imagefactory-plugins-TinMan
    - imagefactory-plugins-Docker
    - imagefactory-plugins-vSphere
    - imagefactory-plugins-ovfcommon
    - imagefactory-plugins
    - imagefactory-plugins-OVA
    - imagefactory-plugins-EC2
    - imagefactory-plugins-RHEVM
    - python-psphere
    - VMDKstream
    - pykickstart
  when: not inventory_hostname.startswith('buildppc-') and ansible_distribution_major_version|int > 21
  tags:
  - koji_builder

- name: build /etc/kojid/kojid.conf from group vars
  template: src=kojid.conf dest=/etc/kojid/kojid.conf
  notify:
  - restart kojid
  tags:
  - koji_builder

- name: build /etc/koji/koji.conf from group vars
  template: src=koji.conf dest=/etc/koji.conf
  tags:
  - koji_builder

- name: config for the kojid runroot plugin (only some builders)
  copy: src=runroot.conf dest=/etc/kojid/runroot.conf
  when: "'runroot' in group_names"
  notify:
  - restart kojid
  tags:
  - koji_builder

- name: Create a directory for plugins.
  file: dest=/usr/lib/koji-hub-plugins state=directory
  when: "'runroot' in group_names"
  tags:
  - koji_builder

- name: Copy in the kojid runroot plugin itself.
  copy: src=runroot.py dest=/usr/lib/koji-hub-plugins/runroot.py
  when: "'runroot' in group_names"
  notify:
  - restart kojid
  tags:
  - koji_builder

# setup for oz/imagefactory
- name: make .psphere dir
  file: state=directory path=/root/.psphere mode=775 owner=root group=root
  tags:
  - koji_builder

- name: make .psphere/templates dir
  file: state=directory path=/root/.psphere/templates mode=775 owner=root group=root
  tags:
  - koji_builder

- name: copy over /root/.psphere/config.yaml
  copy: src={{ private }}/files/koji/config.yaml dest=/root/.psphere/config.yaml
  tags:
  - koji_builder
# done oz/imagefactory

- name: copy over koji ca cert
  copy: src="{{ private }}/files/koji/buildercerts/fedora-ca.cert" dest=/etc/kojid/cacert.pem

- name: copy over /etc/security/limits.conf
  copy: src=limits.conf dest=/etc/security/limits.conf
  tags:
  - koji_builder

- name: copy over builder cert to /etc/kojid/kojibuilder.pem
  copy: src="{{ private }}/files/koji/buildercerts/{{ inventory_hostname }}.pem" dest=/etc/kojid/kojibuilder.pem mode=600   
  tags:
  - koji_builder

# idmapd and make sure it's set to run
- name: idmapd.conf
  copy: src=idmapd.conf dest=/etc/idmapd.conf
  tags:
  - configs
  - koji_builder

# oz.cfg  upstream ram and cpu definitions are not enough
- name: oz.cfg
  copy: src=oz.cfg dest=/etc/oz/oz.cfg
  when: not inventory_hostname.startswith('buildppc')
  tags:
  - koji_builder

- name: make a mnt/koji link
  file: state=link src=/mnt/fedora_koji/koji dest=/mnt/koji
  when: inventory_hostname.startswith(('build','arm04-builder00','arm04-builder01')) and not inventory_hostname.startswith('buildvm-ppc64')
  tags:
  - koji_builder

- name: make a mnt/koji link
  file: state=link src=/mnt/fedora_koji dest=/mnt/koji
  when: inventory_hostname.startswith(('aarch64','ppc8','buildvm-ppc64'))
  tags:
  - koji_builder

# mock configs for pungify job
- name: put extra special  mock configs in
  copy: src=builders/{{ item }} dest="/etc/mock/{{ item }}" mode=644
  with_items:
    - fedora-branched-pungi-armhfp.cfg
    - fedora-branched-pungi-i386.cfg
    - fedora-branched-pungi-x86_64.cfg
    - fedora-branched-pungi-aarch64.cfg
    - fedora-branched-pungi-ppc64.cfg
    - fedora-branched-pungi-ppc64le.cfg
    - fedora-rawhide-pungi-i386.cfg
    - fedora-rawhide-pungi-x86_64.cfg
    - fedora-rawhide-pungi-armhfp.cfg
    - fedora-rawhide-pungi-aarch64.cfg
    - fedora-rawhide-pungi-ppc64.cfg
    - fedora-rawhide-pungi-ppc64le.cfg
  tags:
  - koji_builder

- name: mock site-defaults.cfg
  copy: src=builders/site-defaults.cfg dest=/etc/mock/site-defaults.cfg mode=0644 owner=root group=mock
  when: not inventory_hostname.startswith('bkernel')
  tags:
  - koji_builder

- name: ntp steptickers
  copy: src="{{ files }}/common/step-tickers" dest=/etc/ntp/step-tickers
  tags:
  - koji_builder
  - ntp

- name: ntp.conf
  copy: src="{{ roles }}/base/files/ntp/ntp.conf" dest=/etc/ntp.conf
  tags:
  - koji_builder
  - ntp

#
# We want more loop devices on builders to allow more image creates
#

- name: check for max_loop with grub2
  command: cat /etc/grub2.cfg
  register: max_loop
  always_run: yes
  changed_when: '1 != 1'
  when: ansible_distribution_major_version|int != 6 and ansible_architecture == 'x86_64'
  tags:
  - koji_builder

- name: check for max_loop with grub1
  command: cat /etc/grub.conf
  register: max_loop
  always_run: yes
  changed_when: '1 != 1'
  when: ansible_distribution == 'RedHat' and ansible_architecture == 'x86_64' and ansible_distribution_major_version|int == 6
  tags:
  - koji_builder

- name: set kernel params for more loops
  action: command /sbin/grubby --update-kernel=ALL --args=max_loop=64 
  when: max_loop is defined and max_loop.stdout.find("max_loop=64") == -1
  tags:
  - koji_builder

#
# x86_64 builders run pungify, that needs hfs module in order to make
# The efi/mac images. This module is only needed on rhel. 
#

- name: special pkgs for the x86_64 builders
  yum: state=present pkg={{ item }}
  with_items:
  - kmod-hfsplus    
  when: is_rhel is defined and ansible_architecture == 'x86_64' and ansible_distribution_major_version|int == '6'
  tags:
  - koji_builder
